package everything;

public abstract class Agent {



//	protected static final int FLAG_SCORE = 30;

	protected int _xLocation;
	protected int _yLocation;
	protected String _orientation;
	protected boolean _onIce;
	protected boolean _isGhost;
	protected int _score;
	protected WorldMap _map;
	protected String _name;
	protected String _color;
	protected String _type;

	protected Agent(){

	}

	public Agent(String name, String color, int x, int y, WorldMap map) {
		_xLocation = x;
		_yLocation = y;
		_orientation = "NORTH";
		_name = name;
		_color = color;
		_map = map;
		_onIce = false;
	}

	public String getName() {
		return _name;
	}

	public String getColor() {
		return _color;
	}

	public String getOrientation() {
		return _orientation;
	}

	public void setOrientation(String orientation) {
		_score--;
		_orientation = orientation;
		_map.turnAgent(this, orientation);
	}

	public int getX(){
		return _xLocation;
	}
	public int getY(){
		return _yLocation;
	}


	protected void move(String direction){
		_score--;
		// when on ice, unable to turn
		if (!_onIce){
			_orientation = direction;
		}
		int oldX = _xLocation;
		int oldY = _yLocation;
		if (_orientation.equalsIgnoreCase("North") && ! _map.isMovementBlocked(_xLocation-1, _yLocation)){
			_xLocation--;
			_map.moveAgent(this, _orientation);
			_map.restore(oldX,oldY);
		} else if (_orientation.equalsIgnoreCase("East") && !_map.isMovementBlocked(_xLocation, _yLocation+1)){
			_yLocation++;
			_map.moveAgent(this, _orientation);
			_map.restore(oldX,oldY);
		} else if (_orientation.equalsIgnoreCase("South") && !_map.isMovementBlocked(_xLocation+1,_yLocation)){
			_xLocation++;
			_map.moveAgent(this, _orientation);
			_map.restore(oldX,oldY);
		} else if (_orientation.equalsIgnoreCase("West") && !_map.isMovementBlocked(_xLocation, _yLocation-1)){
			_yLocation--;
			_map.moveAgent(this, _orientation);
			_map.restore(oldX,oldY);
		}
		// found flag
		if (_map.brickStatus(_xLocation, _yLocation) == WorldMap.FLAG && !_isGhost){
			// if _map.isFlag(_xLocation, _yLocation)
			_score+=_map.getFlagValue();
			_map.removeFlag(_xLocation, _yLocation);

		}
		if (_map.isIce(_xLocation, _yLocation)){
			_onIce = true;
			// if near wall, agent stops and is able to turn
			if (_orientation.equalsIgnoreCase("North")){
				if(_map.isMovementBlocked(_xLocation - 1, _yLocation)){
					_onIce = false;
				}
			} else if (_orientation.equalsIgnoreCase("East")){
				if(_map.isMovementBlocked(_xLocation , _yLocation + 1)){
					_onIce = false;
				}
			} else if (_orientation.equalsIgnoreCase("South")){
				if(_map.isMovementBlocked(_xLocation + 1, _yLocation)){
					_onIce = false;
				}
			} else if (_orientation.equalsIgnoreCase("West")){
				if(_map.isMovementBlocked(_xLocation , _yLocation - 1)){
					_onIce = false;
				}
			}
		} else {
			_onIce = false;
		}
		_map.updateScore(this);
	}

	public abstract void makeMove();

	public abstract boolean isKilling(int x, int y, WorldMap map);

	public boolean isGhost() {
		return _isGhost;
	}

	public void die(){
		_isGhost = true;
	}

	public int getScore() {
		return _score;
	}

	public String getType() {
		return _type;
	}
}

